#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x;
}
inline bool readB(){
	char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	return ch&1;
}
inline void write(int x){
	if(x==0){putchar('0');return;}
	char ch[20];int cnt=-1;
	while(x){
		ch[++cnt]=(x%10)^48;
		x/=10;
	}
	for(int i=cnt;~i;--i) putchar(ch[i]);
}
int n,m,q[100005],K,T;
int _a[131072],a[131072];
int mxq,ans[100005];
bool d[18][131072];
int fr[262144];
int s[262144][18],top[262144];
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	n=read();m=read();
	while((1<<K)<n) ++K;
	for(int i=1;i<=n;++i) _a[i]=read();
	for(int i=1;i<=m;++i) q[i]=read(),mxq=max(mxq,q[i]);
	for(int i=1;i<=K;++i)
		for(int j=0;j<(1<<(K-i));++j) d[i][j]=readB();
	T=read();
	while(T--){
		int X[4];
		for(int i=0;i<4;++i) X[i]=read();
		for(int i=1;i<=n;++i) a[i]=min(_a[i]^X[i&3],K);
		for(int j=0;j<(1<<K+1);++j) top[j]=0;
		for(int j=1<<K;j<(1<<(K+1));++j) fr[j]=j-(1<<K)+1;
		for(int curd=1;curd<=K;++curd){
			for(int cur=1<<(K-curd);cur<(1<<(K-curd+1));++cur){
				int ls=cur<<1,rs=cur<<1|1;
				fr[cur]=fr[ls]+fr[rs];
			}
		}
		for(int i=1;i<=mxq;++i){
			int cur=1<<K|(i-1);
			fr[cur]=0;
			s[cur][top[cur]=1]=i;
			int dep;
			if(i==1) dep=0;
			else dep=__lg(i-1)+1;
			for(int curd=1;curd<=dep;++curd){
				cur>>=1;
				int ls=cur<<1,rs=cur<<1|1;
				if(d[curd][(i-1)>>curd]) ls^=1,rs^=1;
				fr[cur]=fr[ls];
				int tp=0;
				bool flag=0;
				for(int j=1;j<=top[ls];++j){
					if(a[s[ls][j]]>=curd) s[cur][++tp]=s[ls][j];
					else flag=1;
				}
				if(fr[ls]) flag=1;
				if(flag){
					fr[cur]+=fr[rs];
					for(int j=1;j<=top[rs];++j) s[cur][++tp]=s[rs][j];
				}
				top[cur]=tp;
			}
			ans[i]=fr[cur];
			for(int j=1;j<=top[cur];++j) ans[i]+=s[cur][j];
		}
		int res=0;
		for(int i=1;i<=m;++i) res^=i*ans[q[i]];
		write(res);putchar('\n');
	}
	return 0;
}

/*
5 5
0 0 0 0 0
5 4 1 2 3
1001
10
1
1
2 1 0 0

*/